package com.zhuxulin.lottery.fragments;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.zhuxulin.lottery.App;
import com.zhuxulin.lottery.R;
import com.zhuxulin.lottery.info.API;
import com.zhuxulin.lottery.info.Info;
import com.zhuxulin.lottery.net.ICall;
import com.zhuxulin.lottery.net.Request;
import com.zhuxulin.lottery.net.Server;
import com.zhuxulin.lottery.net.Request.Method;
import com.zhuxulin.lottery.utils.CheckUtils;
import com.zhuxulin.lottery.utils.LogUtils;

public class LoginFragment extends Fragment implements View.OnClickListener {
	private static String TAG = LoginFragment.class.getSimpleName();
	/** 用户名输入框 */
	private EditText name = null;
	/** 用户名 */
	private String userName = null;
	/** 密码输入框 */
	private EditText pwd = null;
	/** 密码 */
	private String password = null;
	/** 登陆按钮 */
	private Button login = null;
	/** 网络请求圆形处理 */
	private ProgressDialog pBar = null;
	private Dialog dialog = null;
	/** 网络请求的服务类 */
	private Server server = null;
	/** 网络请求类 */
	private Request request = null;

	public LoginFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main, container, false);
		name = (EditText) view.findViewById(R.id.user);
		pwd = (EditText) view.findViewById(R.id.pwd);
		login = (Button) view.findViewById(R.id.login);
		server = Server.getInstance(getActivity());
		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		login.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		userName = name.getText().toString().trim();
		password = pwd.getText().toString().trim();
		pBar = new ProgressDialog(getActivity());
		if (v.getId() == R.id.login) {
			if (CheckUtils.isEmpty(userName)) {
				Toast.makeText(getActivity(), getString(R.string.UserNameEmpty), Toast.LENGTH_LONG).show();
				return;
			}
			if (CheckUtils.isEmpty(password)) {
				Toast.makeText(getActivity(), getString(R.string.PasswordEmpty), Toast.LENGTH_LONG).show();
				return;
			}
			pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pBar.setView(getView());
			pBar.show();
			new Thread(new Runnable() {
				@Override
				public void run() {
					request = new Request(Method.GET, Info.URI_LOGIN);
					request.setHost(App.HOST);
					request.putParam("username", userName);
					request.putParam("password", password);
					server.execute(request, new ICall() {
						@Override
						public void onResult(String msg, int status) {
							LogUtils.i(TAG, msg);
						}
					});

					request = new Request(Method.GET, Info.URI_GET_ORDER);
					request.setHost(App.HOST);
					request.putParam("username", userName);
					request.putParam("password", password);
					request.putParam("orderNumber", "1404281437031910");
					server.execute(request, new ICall() {
						@Override
						public void onResult(String msg, int status) {
							LogUtils.i(TAG, msg);
						}
					});

					request = new Request(Method.GET, Info.URI_ORDER);
					request.setHost(App.HOST);
					request.putParam("username", userName);
					request.putParam("password", password);
					request.putParam("number", "1563B1B2,1563B6B0");
					request.putParam("date", "D");
					request.putParam("company", "KMT");
					server.execute(request, new ICall() {
						@Override
						public void onResult(String msg, int status) {
							LogUtils.i(TAG, msg);
						}
					});

				}
			}).start();
			return;
		}
	}
}